package qe.spider.grab;

import cn.hutool.core.text.UnicodeUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import qe.spider.vo.PlateList;

import java.io.IOException;

//行情-板块列表-精选
@Slf4j
public class GrabKPLJingXuanPlate {

    private String userID;
    private String token;

    public GrabKPLJingXuanPlate(String userID, String token) {
        this.userID = userID;
        this.token = token;
    }

    public static void main(String[] args) throws IOException {
        new GrabKPLJingXuanPlate("111775r", "13f1764f728c8db11f85b98e524bec3c").grab();
    }

    private FormBody formBody() {
        FormBody formBody = new FormBody.Builder()
                .add("c", "PCArrangeData")
                .add("a", "GetZSIndexPlate")
                .add("SelType", "2")
//                .add("ZSType", "5")//4是行业板块，5是概念板块，7是精选板块，6是地区板块
//                .add("PType", "5")  //5是主力净额排序，2是涨幅，3是涨速，9是流通市值
                .add("ZSType", "7")//4是行业板块，5是概念板块，7是精选板块，6是地区板块
                .add("PType", "5")//5是主力净额排序，2是涨幅，3是涨速，9是流通市值
                .add("POrder", "1")//1是降序，0是升序
                .add("PStart", "")
                .add("PEnd", "")
                .add("PIndex", "0")//分页索引
                .add("Pst", "500")//每页数据条数
                .add("UserID", userID)
                .add("Token", token).build();
        return formBody;
    }

    public PlateList grab() throws IOException {
        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder()
                .url("https://pchq.kaipanla.com/w1/api/index.php")
//                .header("Accept-Encoding", "gzip, deflate, br")
                .header("Accept-Encoding", "br")
                .header("Accept", "application/json, text/javascript, */*; q=0.01")
                .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
                .header("Cache-Control", "no-cache")
                .header("Connection", "keep-alive")
                .header("Content-Length", "152")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Host", "pchq.kaipanla.com")
                .header("Origin", "https://www.kaipanla.com")
                .header("Pragma", "no-cache")
                .header("Referer", "https://www.kaipanla.com/")
                .header("Sec-Fetch-Dest", "empty")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Site", "same-site")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0")
                .header("sec-ch-ua", "\"Microsoft Edge\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .post(formBody())
                .build();
        Gson gson = new Gson();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

//            byte[] bytes = response.body().bytes();
            String result = UnicodeUtil.toString(response.body().string());
            log.info(result);
            PlateList list = gson.fromJson(result, PlateList.class);
            list.getPlates().refreshPlates( "精选");
            log.debug(list.toString());
            return list;
        }

    }


}
